package org.example.meetingsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.meetingsystem.VO.CancellationRequestVO;
import org.example.meetingsystem.entity.CancellationRequest;
import org.example.meetingsystem.enums.CancellationStatus;

import java.util.List;

@Mapper
public interface CancellationRequestMapper extends BaseMapper<CancellationRequest> {

    /**
     * 查询待处理的取消申请
     */
    @Select("SELECT cr.id, cr.reason, cr.refund_rate, cr.refund_amount, cr.status, " +
            "cr.processed_by, cr.processed_at, cr.created_at, " +
            "r.start_time, r.end_time, mr.name as room_name " +
            "FROM cancellation_requests cr " +
            "LEFT JOIN reservations r ON cr.reservation_id = r.id " +
            "LEFT JOIN meeting_rooms mr ON r.room_id = mr.id " +
            "WHERE cr.status = 'PENDING' " +
            "ORDER BY cr.created_at DESC")
    List<CancellationRequest> selectPendingRequests();

    /**
     * 分页查询取消申请及关联信息
     */
    @Select("<script>" +
            "SELECT " +
            "    cr.id, cr.reason, " +
            "    cr.refund_rate, cr.refund_amount, cr.status, " +
            "    cr.processed_by, cr.processed_at, cr.created_at, " +
            "    r.start_time, " +
            "    r.end_time, " +
            "    mr.name as room_name " +
            "FROM cancellation_requests cr " +
            "LEFT JOIN reservations r ON cr.reservation_id = r.id " +
            "LEFT JOIN meeting_rooms mr ON r.room_id = mr.id " +
            "<where>" +
            "    <if test='status != null'>" +
            "        AND cr.status = #{status}" +
            "    </if>" +
            "</where>" +
            "ORDER BY cr.created_at DESC" +
            "</script>")
    IPage<CancellationRequestVO> selectCancellationRequestsWithDetails(
            IPage<?> page,
            @Param("status") CancellationStatus status
    );
}
